package com.hook3.pianke.view;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hook3.pianke.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2015/12/11.
 */
public class ReadDetalsCommon {


    public Activity activity;
    public String sort;
    public String type;
    public mJson oJson;
    public View view;
    public ListView listView;
    public ReadDetalsCommon(Activity activity, String type, String sort) {
       this.activity = activity;
        this.type = type;
        this.sort = sort;
//        initView();
//        initData();
    }

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public mJson getoJson() {
        return oJson;
    }

    public void setoJson(mJson oJson) {
        this.oJson = oJson;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }



    public View initView() {
        view = View.inflate(activity, R.layout.read_gridview_details_common, null);
        listView = (ListView) view.findViewById(R.id.lv_read_gridview_common1);
        return view;
    }


    public void initData() {


        RequestParams params = new RequestParams("http://api2.pianke.me/read/columns_detail");
        Log.e("TAG","联网已操作");
        params.addBodyParameter("sort", sort);
        params.addBodyParameter("start", "0");
        params.addBodyParameter("client", "2");
        params.addBodyParameter("typeid", type);
        params.addBodyParameter("limit", "10");
        x.http().post(params, new Callback.CacheCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                oJson = gson.fromJson(result, mJson.class);
                Log.e("TAG","联网成功");

                listView.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return oJson.data.list.size();
                    }

                    @Override
                    public Object getItem(int position) {
                        return oJson.data.list.get(position);
                    }

                    @Override
                    public long getItemId(int position) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder viewHolder = new ViewHolder();
                        if (convertView == null) {

                            convertView = View.inflate(activity, R.layout.item_read_gridview_details, null);

                            viewHolder.iv_morning_item_read_gridview = (ImageView) convertView.findViewById(R.id.iv_morning_item_read_gridview);
                            viewHolder.tv_title_item_read_gridview = (TextView) convertView.findViewById(R.id.tv_title_item_read_gridview);
                            viewHolder.tv_content_item_read_gridview = (TextView) convertView.findViewById(R.id.tv_content_item_read_gridview);
                            convertView.setTag(viewHolder);
                        } else {
                            viewHolder = (ViewHolder) convertView.getTag();
                        }

                        x.image().bind(viewHolder.iv_morning_item_read_gridview, oJson.data.list.get(position).coverimg);
                        viewHolder.tv_title_item_read_gridview.setText(oJson.data.list.get(position).title);
                        viewHolder.tv_content_item_read_gridview.setText(oJson.data.list.get(position).content);
                        return convertView;
                    }

                    class ViewHolder {
                        public ImageView iv_morning_item_read_gridview;
                        public TextView tv_title_item_read_gridview;
                        public TextView tv_content_item_read_gridview;
                    }
                });


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });


    }

    class mJson {
        public Data data;
        public String result;
    }

    class Data {
        public ColumnsInfo columnsInfo;
        public List<ListDetails> list;
    }

    class ColumnsInfo {
        public String typeid;
        public String typename;
    }

    class ListDetails {
        public String content;
        public String coverimg;
        public String id;
        public String name;
        public String title;
    }

}


